﻿namespace  Codeplex.KinkumaFramework.Interactivity
{
    using System.Windows;
    using System.Windows.Interactivity;

    /// <summary>
    /// TriggerBaseがアクションを実行する前に確認ダイアログで実行可否を選択可能なようにするBehaviorです。
    /// </summary>
    public class ConfirmPreviewActionBehavior : Behavior<System.Windows.Interactivity.TriggerBase>
    {
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register(
                "Title",
                typeof(string),
                typeof(ConfirmPreviewActionBehavior),
                new PropertyMetadata("確認"));

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register(
                "Message", 
                typeof(string), 
                typeof(ConfirmPreviewActionBehavior), 
                new PropertyMetadata(string.Empty));

        /// <summary>
        /// 確認メッセージ
        /// </summary>
        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        /// <summary>
        /// 確認ダイアログのタイトル
        /// </summary>
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.PreviewInvoke += PreviewInvoke;
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.PreviewInvoke -= PreviewInvoke;
            base.OnDetaching();
        }
 
        private void PreviewInvoke(object sender, PreviewInvokeEventArgs e)
        {
            e.Cancelling = MessageBox.Show(
                this.Message,
                this.Title,
                MessageBoxButton.OKCancel) == MessageBoxResult.Cancel;
        }

    }
}
